{extend name="common/base"/}
{block name="style"}
<style>
	.upload-img {
		width: 120px;
		height: 90px;
		overflow: hidden;
		position: relative;
		border: 1px solid #eee;
		padding: 1px;
		margin: 5px;
		float: left;
	}

	.upload-close {
		position: absolute;
		top: 1px;
		right: 1px;
	}

	.editormd-code-toolbar select {
		display: inline-block
	}

	.editormd li {
		list-style: inherit;
	}
</style>
{/block}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-3">
	<table class="layui-table layui-table-form">
		<tr>
			<td class="layui-td-gray">页面标题 <font>*</font></td>
			<td colspan="3"><input type="text" name="title" lay-verify="required" lay-reqText="请输入页面标题" placeholder="请输入页面标题" class="layui-input" value="{$detail.title}"></td>
			<td class="layui-td-gray" rowspan="3">缩略图</td>
			<td rowspan="3" style="vertical-align:top">
				<div class="layui-upload" style="text-align:center;">
					<button type="button" class="layui-btn layui-btn-sm" id="uploadBtn">上传封面图(尺寸：750x560)</button>
					<div class="layui-upload-list" id="demo1">
						<img src="{$detail.thumb|get_file}" style="width:200px;max-width:200px" />
						<input type="hidden" name="thumb" value="{$detail.thumb}">
					</div>
				</div>
			</td>		
		</tr>
		<tr>
			<td class="layui-td-gray">关键字 <font>*</font></td>
			<td colspan="3">
				<input type="text" id="keyword_name" name="keyword_names" lay-verify="required" lay-reqText="请选择关键字"
					placeholder="请选择关键字" class="layui-input" value="{$detail.keyword_names}" readonly>
				<input type="hidden" id="keyword_id" name="keyword_ids" value="{$detail.keyword_ids}">
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray" style="vertical-align:top;">页面摘要</td>
			<td colspan="3">
				<textarea name="desc" placeholder="请输入页面摘要，200字以内" class="layui-textarea">{$detail.desc}</textarea>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">图集相册</td>
			<td colspan="5">
				<div class="layui-upload">
					<button type="button" class="layui-btn layui-btn-sm" id="uploadBtn2">上传图片</button>
					<div class="layui-upload-list" id="demo2">
						<input type="hidden" name="banner" value="{$detail.banner}">
						{notempty name="detail.banner"}
						{volist name="detail.banner_array" id="vo"}
						<div class="upload-img img-cover" id="uploadImg{$vo}"><div class="gg-img-cover cover-4-3"><img src="{$vo|get_file}"><div class="upload-close"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delimg" data-id="{$vo}">删除</a></div></div></div>
						{/volist}
						{/notempty}
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">展示模板 <font>*</font></td>
			<td>
				<select name="template" lay-verify="required" lay-reqText="请选择前台展示模板">
					<option value="">请选择前台展示模板</option>
					{volist name="$templates" id="vo"}
					<option value="{$vo.filename}" {eq name="$vo.filename" value="$detail.template" }selected{/eq}>{$vo.basename}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray-2">URL文件名称</td>
			<td>
				<input type="text" name="name" value="{$detail.name}" placeholder="请输入URL文件名称" class="layui-input">
			</td>
			<td class="layui-td-gray">状态 <font>*</font></td>
			<td>
				<input type="radio" name="status" value="1" title="正常" {eq name="$detail.status" value="1" }checked{/eq}>
				<input type="radio" name="status" value="0" title="下架" {eq name="$detail.status" value="0" }checked{/eq}>
			</td>
		</tr>
		{empty name="$detail.md_content"}
		<tr>
			<td colspan="6" class="layui-td-gray" style="text-align:left">页面内容<font>*</font></td>
		</tr>
		<tr>
			<td colspan="6">
				<textarea class="layui-textarea" id="container">{$detail.content}</textarea>
			</td>
		</tr>
		{else/}
		{eq name="$editor" value="1"}
		<tr>
			<td colspan="6" class="layui-td-gray" style="text-align:left">页面内容<font>*</font><span
					style="margin-left:30px; color: red">当前为TinyMCE富文本编辑器，可在【系统管理->其他配置】中切换为mardown编辑器</span></td>
		</tr>
		<tr>
			<td colspan="6">
				<textarea placeholder="请输入内容" class="layui-textarea" id="container">{$detail.content}</textarea>
			</td>
		</tr>
		{else/}
		<tr>
			<td colspan="6" class="layui-td-gray" style="text-align:left">页面内容<font>*</font><span
					style="margin-left:30px; color: red">当前为mardown编辑器，可在【系统管理->其他配置】中切换为TinyMCE富文本编辑器</span></td>
		</tr>
		<tr>
			<td colspan="6">
				<div>
					<textarea id="mdContent" style="display:none;">{$detail.md_content|raw}</textarea>
					<div id="docContent"></div>
				</div>
			</td>
		</tr>
		{/eq}
		{/empty}
	</table>
	<div class="py-3">
		<input type="hidden" name="id" value="{$detail.id}">
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		<button lay-event="back" class="layui-btn layui-btn-primary">关闭</button>
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	// 查找指定的元素在数组中的位置
	Array.prototype.indexOf = function (val) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == val) {
				return i;
			}
		}
		return -1;
	};
	// 通过索引删除数组元素
	Array.prototype.remove = function (val) {
		var index = this.indexOf(val);
		if (index > -1) {
			this.splice(index, 1);
		}
	};

	const editorType = '{$editor}';
	var moduleInit;
	if (editorType == 1) {
		moduleInit = ['tool', 'tagpicker', 'tinymce'];
	}
	else {
		moduleInit = ['tool', 'tagpicker', 'editormd'];
	}

	function gouguInit() {
		var form = layui.form, tool = layui.tool, tagspicker = layui.tagpicker, upload = layui.upload;

		var tags = new tagspicker({
			'url': '/admin/api/get_keyword_cate',
			'target': 'keyword_name',
			'tag_ids': 'keyword_id',
			'tag_tags': 'keyword_name',
			'height': 500,
			'isDiy': 1
		});

		//缩略图上传
		var uploadInst = upload.render({
			elem: '#uploadBtn'
			, url: '/admin/api/upload'
			, done: function (res) {
				//如果上传失败
				if (res.code == 1) {
					return layer.msg('上传失败');
				}
				//上传成功
				$('#demo1 input').attr('value', res.data.id);
				$('#demo1 img').attr('src', res.data.filepath);
			}
		});

		//广告图上传
		var uploadInst2 = upload.render({
			elem: '#uploadBtn2'
			, url: '/admin/api/upload'
			, done: function (res) {
				//如果上传失败
				if (res.code == 1) {
					return layer.msg('上传失败');
				}
				//上传成功
				var idsStr = $('#demo2 input').val();
				var idsArray = [];
				if (idsStr != '') {
					idsArray = idsStr.split(",");
				}
				idsArray.push(res.data.id);
				$('#demo2 input').attr('value', idsArray.join(','));		
				$('#demo2').append('<div class="upload-img img-cover" id="uploadImg' + res.data.id + '"><div class="gg-img-cover cover-4-3"><img src="' + res.data.filepath + '"><div class="upload-close"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delimg" data-id="' + res.data.id + '">删除</a></div></div></div>');
			}
		});

		$('#demo2').on('click', '[lay-event="delimg"]', function () {
			var _id = $(this).data('id');
			var idsStr = $('#demo2 input').val();
			var idsArray = [];
			if (idsStr != '') {
				idsArray = idsStr.split(",");
			}
			idsArray.remove(_id);
			$('#demo2 input').attr('value', idsArray.join(','));
			$('#uploadImg' + _id).remove();
		})

		//监听返回
		$('body').on('click', '[lay-event="back"]', function () {
			tool.tabClose();
			return false;
		});

		if (editorType == 1) {
			var editor = layui.tinymce;
			var edit = editor.render({
				selector: "#container",
				height: 500
			});
			//监听提交
			form.on('submit(webform)', function (data) {
				data.field.content = tinyMCE.editors['container'].getContent();
				if (data.field.content == '') {
					layer.msg('请先完善页面内容');
					return false;
				}
				let callback = function (e) {
					if (e.code == 0) {
						tool.tabRefresh(80);
						layer.confirm('保存成功,关闭当前页面吗?', { icon: 3, title: '提示' }, function (index) {
							tool.tabClose();
						});
					} else {
						layer.msg(e.msg);
					}
				}
				tool.post("/admin/pages/add", data.field, callback);
				return false;
			});
		}
		else {
			var editor = layui.editormd;
			var edit = editor.render('docContent', {
				markdown: document.getElementById('mdContent').value
			});
			//监听提交
			form.on('submit(webform)', function (data) {
				if (data.field.mdContent == '') {
					layer.msg('请先完善页面内容');
					return false;
				}
				let callback = function (e) {
					if (e.code == 0) {
						tool.tabRefresh(80);
						layer.confirm('保存成功,关闭当前页面吗?', { icon: 3, title: '提示' }, function (index) {
							tool.tabClose();
						});
					} else {
						layer.msg(e.msg);
					}
				}
				tool.post("/admin/pages/add", data.field, callback);
				return false;
			});
		}
	}
</script>
{/block}
<!-- /脚本 -->